#!/bin/sh -ef

cd /.image
mkdir -p boot

case "`arch`" in
	e2k)
		kname=image
		;;
	*)
		kname=vmlinuz
		;;
esac

if [ -L "/boot/$kname" ]; then
	kimage="$(readlink -ev /boot/$kname)"
	kverdef="${kimage#/boot/$kname-}"
fi

kimage="$(find /boot -type f -name "$kname-*")"
initrd="$(find /boot -type f -name 'initrd-*.img')"

cp $verbose -af $kimage boot/
mv boot/$kname-$kverdef boot/$kname
cp $verbose -af $initrd boot/
mv boot/initrd-$kverdef.img boot/initrd.img

# copy dtb for default kernel
if [ -n "$GLOBAL_COPY_DTB" ]; then
	dtb_dir=
	[ -d /boot/devicetree/$kverdef ] && dtb_dir=/boot/devicetree/$kverdef
	[ -z "$dtb_dir" ] && [ -d /lib/devicetree/$kverdef ] &&
		dtb_dir=/lib/devicetree/$kverdef
	if [ -n "$dtb_dir" ]; then
		cp -r "$dtb_dir/" dtb
		# cleanup symlinks which are duplicated for compatibility
		find dtb/ -type l -delete
		# fix permissions for copy-tree
		chmod -R 755 dtb
	else
		echo "dtb directory for $kverdef not found"
	fi
fi
